package ssm.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import ssm.dao.IPermissionDao;
import ssm.domain.Permission;
import ssm.domain.UserInfo;
import ssm.service.IPermissionService;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController  {

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/deletePermission.do")
    public String deletePermission(String id) throws Exception {

//        此处删除完毕后，直接跳转到查询页面
        permissionService.deleteById(id);

        return "redirect:findAll.do";
    }


    @RequestMapping("/findById.do")
    public ModelAndView findById(String id) throws Exception {
        Permission byId = permissionService.findById(id);

        ModelAndView mv = new ModelAndView();
        mv.addObject("permission",byId);
        mv.setViewName("permission-show");

        return mv;
    }

    @RequestMapping("/findAll.do")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();

        List<Permission> list = permissionService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("permission-list");

        return mv;
    }

    @RequestMapping("/save.do")
    public String Save(Permission permission){
        permissionService.save(permission);

        return "redirect:findAll.do";
    }
}
